# May 25, 2021

# This file creates the tables included in the manuscript and appendix

# Install/load packages
source("loadPackages.R")
loadPackages(c("dplyr", "purrr", "readr", "stargazer", "stringr"))

############################################################
# LOAD FILES

# Please ensure that the working directory is set to the replication folder

# read csv files into environment
combined.df.full <- read_csv("EquilibriumExperimentCombined.csv")
continuation.values <- read_csv("ContValueCalculations.csv")


# below we run the analysis without using the endogenous treatments
combined.df <- combined.df.full %>% filter(str_detect(treatment2, "RS", negate = TRUE))
equilibrium <- continuation.values %>% filter(str_detect(Name, "RS", negate = TRUE))



###############################################################
# MANUSCRIPT TABLES
###############################################################


# MANUSCRIPT TABLE 2
# the output for these is constructed in DataPreparation.R and outputted as "equilibrium.values"
equilibrium %>% select(Treatment = Name, Round = Rounds, V.O, V.R, MaxCost.O, Accept) %>% 
  mutate(Decision = ifelse(Accept, "Accept", "Reject")) %>% select(-Accept)


# MANUSCRIPT TABLE 3
reg1 <- combined.df %>%  
  mutate(receiverDecision = ifelse(receiverDecision == "ACCEPT", 1, 0)) %>%
  filter(!(roundId == 9)) %>%  split(.$roundId) %>% 
  map(~ glm(receiverDecision ~  factor(treatment2), data = .,  family = binomial))




stargazer(reg1, type = "latex",
          title = "Logistic Regression Predicting Probability of Acceptance, by Round and Treatment",
          label = "tab:reg1ProbAcceptance",
          covariate.labels = c("CP", "CP-VN", "VN"),
          dep.var.caption = c("Probability of Acceptance"),
          dep.var.labels = c("Round"),
          colnames = TRUE,
          column.labels =  as.character(c(1:8)),
          model.numbers = FALSE,
          out = "REG_Predict_Offer_Acceptance.tex")


###############################################################
# APPENDIX TABLES
###############################################################

# APPENDIX TABLE 1

reg3a <- combined.df %>% 
  mutate(roundId = roundId + 1) %>% 
  filter(roundId < 5) %>%
  split(.$roundId) %>%
  map(~ lm(., formula = "offer_diff ~ treatment2")) 

stargazer(reg3a, type = "latex",
            dep.var.caption = "Difference between Observed and Equilibrium Offers",
            title = "Difference between Observed and Equilibrium Offers, by Round (1-4) and Treatment",
            label = "tab:reg3aOfferDiff",
            dep.var.labels = c("Round"),
            colnames = TRUE,
            column.labels =  as.character(c(1:4)),
            covariate.labels = c("CP", "CP-VN", "VN"),
            model.numbers = FALSE,
            out = "REG_APP_Observed_Equilibrium.tex")

            
# APPENDIX TABLE 2

rejectedByTreatment <- combined.df %>%  filter(roundId < 4) %>%
  mutate(receiverDecision = ifelse(receiverDecision == "ACCEPT", 1, 0)) %>%
  filter(!(roundId == 9), receiverDecision == 1) %>%  split(.$roundId) %>% 
  discard(~ length(unique(.$treatment2)) == 1) %>%
  map(~ lm(., formula = "offer_diff ~ factor(treatment2)")) 


stargazer(rejectedByTreatment,  type = "latex",
            dep.var.caption = "Difference between Rejected and Equilibrium Offer",
            label = "tab:reg4aRejected_1_4",
            dep.var.labels = c("Round"),
            colnames = TRUE,
            column.labels =  as.character(c(1:4)),
            covariate.labels = c("CP", "CP-VN", "VN"),
            model.numbers = FALSE,
            title = "Difference between Rejected and Equilibrium Offer, by Round (1-4) and Treatment",
            out = "REG_APP_Rejected_Accepted_Difference.tex")


